<?php 
class ControllerInformationContact extends Controller {
	private $error = array(); 
	    
  	public function index() {
		$this->language->load('information/contact');
		$this->load->model('tool/seo_url');	
		$this->data['template'] = $this->config->get('config_template');
        
    	$this->document->title = $this->language->get('meta_title');  
        $this->document->keywords = $this->config->get('config_meta_keywords');
        $this->document->description = $this->config->get('config_meta_description');
	 	
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
    		$subject = (trim($this->request->post['subject']) != '') ? $this->request->post['subject'] : $this->language->get('email_subject');
    		$message = (trim($this->request->post['enquiry']) != '') ? $this->request->post['enquiry'] : $this->language->get('email_message');
    		
            
			$mail = new Mail();
			$mail->protocol = $this->config->get('config_mail_protocol');
			$mail->parameter = $this->config->get('config_mail_parameter');
			$mail->hostname = $this->config->get('config_smtp_host');
			$mail->username = $this->config->get('config_smtp_username');
			$mail->password = $this->config->get('config_smtp_password');
			$mail->port = $this->config->get('config_smtp_port');
			$mail->timeout = $this->config->get('config_smtp_timeout');				
			$mail->setTo($this->config->get('config_email'));
	  		$mail->setFrom($this->request->post['email']);
	  		$mail->setSender($this->request->post['name']);
	  		//$mail->setSubject(sprintf($this->language->get('email_subject'), $this->request->post['subject']));
	  		//$mail->setText(strip_tags(html_entity_decode($this->request->post['enquiry'], ENT_QUOTES, 'UTF-8')));
	  		$mail->setSubject($subject);
	  		$mail->setText(strip_tags(html_entity_decode($message, ENT_QUOTES, 'UTF-8')));
      		$mail->send();	      		
      		/*
            $mail = new PHPGMailer();
            $mail->Mailer = $this->config->get('config_mail_protocol');
            $mail->Username = $this->config->get('config_smtp_username');
            $mail->Password = $this->config->get('config_smtp_password');;
            $mail->From = $this->request->post['email'];
            $mail->FromName = $this->request->post['name'];
            $mail->AddAddress($this->config->get('config_email'));
            $mail->Subject = $subject;            
            $mail->Body = strip_tags(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
            $mail->Send();
             */
	  		$this->redirect($this->model_tool_seo_url->rewrite(HTTPS_SERVER . 'index.php?route=information/contact/success'));
    	}

      	$this->document->breadcrumbs = array();

      	$this->document->breadcrumbs[] = array(
        	'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=common/home'),
        	'text'      => $this->language->get('text_home'),
        	'separator' => FALSE
      	);

      	$this->document->breadcrumbs[] = array(
        	'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=information/contact'),
        	'text'      => $this->language->get('heading_title'),
        	'separator' => $this->language->get('text_separator')
      	);	
			
    	$this->data['heading_title'] = $this->language->get('heading_title');

    	$this->data['text_address'] = $this->language->get('text_address');
    	$this->data['text_telephone'] = $this->language->get('text_telephone');
    	$this->data['text_fax'] = $this->language->get('text_fax');

    	$this->data['entry_name'] = $this->language->get('entry_name');
    	$this->data['entry_email'] = $this->language->get('entry_email');
    	$this->data['entry_enquiry'] = $this->language->get('entry_enquiry');
    	$this->data['entry_subject'] = $this->language->get('entry_subject');
		$this->data['entry_captcha'] = $this->language->get('entry_captcha');        
        
		if (isset($this->error['name'])) {
    		$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = '';
		}
		
		if (isset($this->error['email'])) {
			$this->data['error_email'] = $this->error['email'];
		} else {
			$this->data['error_email'] = '';
		}		
		
		if (isset($this->error['enquiry'])) {
			$this->data['error_enquiry'] = $this->error['enquiry'];
		} else {
			$this->data['error_enquiry'] = '';
		}	

		if (isset($this->error['subject'])) {
			$this->data['error_subject'] = $this->error['subject'];
		} else {
			$this->data['error_subject'] = '';
		}	
		
 		if (isset($this->error['captcha'])) {
			$this->data['error_captcha'] = $this->error['captcha'];
		} else {
			$this->data['error_captcha'] = '';
		}	

    	$this->data['button_continue'] = $this->language->get('button_continue');
		
		$this->load->model('setting/tooltip');              
        $this->data['contact_text'] = $this->model_setting_tooltip->getContactText();
		$this->data['action'] = HTTP_SERVER . 'index.php?route=information/contact';
		$this->data['store'] = $this->config->get('config_name');
    	$this->data['contact_address'] = nl2br($this->config->get('config_address'));
    	$this->data['contact_telephone'] = $this->config->get('config_telephone');
    	$this->data['contact_fax'] = $this->config->get('config_fax');
		$this->data['contact_email'] = $this->getEmail();
    	
		if (isset($this->request->post['name'])) {
			$this->data['name'] = $this->request->post['name'];
		} else {
			$this->data['name'] = '';
		}

		if (isset($this->request->post['email'])) {
			$this->data['email'] = $this->request->post['email'];
		} else {
			$this->data['email'] = '';
		}
		
		if (isset($this->request->post['enquiry'])) {
			$this->data['enquiry'] = $this->request->post['enquiry'];
		} else {
			$this->data['enquiry'] = '';
		}
		
  		if (isset($this->request->post['subject'])) {
			$this->data['subject'] = $this->request->post['subject'];
		} else {
			$this->data['subject'] = '';
		}
		
		if (isset($this->request->post['captcha'])) {
			$this->data['captcha'] = $this->request->post['captcha'];
		} else {
			$this->data['captcha'] = '';
		}			
                
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/information/contact.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/information/contact.tpl';
		} else {
			$this->template = 'default/template/information/contact.tpl';
		}
		
		$this->children = array(
			//'common/column_right',
			'common/footer',
			'common/column_left',
			'common/header',
			'module/services',
            'module/newsletter'
		);
		
 		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));		
  	}

  	public function success() {
		$this->language->load('information/contact');
		$this->load->model('tool/seo_url');				
		$this->data['template'] = $this->config->get('config_template');
        
		$this->document->title = $this->language->get('meta_title'); 
        $this->document->keywords = $this->config->get('config_meta_keywords');
        $this->document->description = $this->config->get('config_meta_description');

      	$this->document->breadcrumbs = array();

      	$this->document->breadcrumbs[] = array(
        	'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=common/home'),
        	'text'      => $this->language->get('text_home'),
        	'separator' => FALSE
      	);

      	$this->document->breadcrumbs[] = array(
        	'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=information/contact'),
        	'text'      => $this->language->get('heading_title'),
        	'separator' => $this->language->get('text_separator')
      	);	
		
    	$this->data['heading_title'] = $this->language->get('heading_title');
    	
    	$str_telefon = isset($this->request->post['telephone'])?"<br />Telefon nr.:".$this->request->post['telephone']:'';

    	$this->data['text_message'] = "Tak for din henvendelse! Vi vil kontakte dig hurtigst muligt ".$str_telefon."<br /><br />Med venlig hilsen<br />Havhøkeren Gilleleje";//$this->language->get('text_message');

    	$this->data['button_continue'] = $this->language->get('button_continue');

    	$this->data['continue'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=common/home');

        $this->data['contact_info']['address'] = nl2br($this->config->get('config_address'));
    	$this->data['contact_info']['telephone'] = $this->config->get('config_telephone');
    	$this->data['contact_info']['fax'] = $this->config->get('config_fax');
		$this->data['contact_info']['email'] = $this->getEmail();
        
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/success.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/success.tpl';
		} else {
			$this->template = 'default/template/common/success.tpl';
		}      		
        
		$this->children = array(
			//'common/column_right',
			'common/footer',
			'common/column_left',
			'common/header',
            'module/services',
            'module/newsletter'
		);
		
 		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression')); 
	}

	public function captcha() {
		$this->load->library('captcha');
		
		$captcha = new Captcha();
		
		$this->session->data['captcha'] = $captcha->getCode();
		
		$captcha->showImage();
	}
	
  	private function validate() {
    	if ((strlen(utf8_decode($this->request->post['name'])) < 1) || (strlen(utf8_decode($this->request->post['name'])) > 32)) {
      		$this->error['name'] = $this->language->get('error_name');
    	}

		$pattern = '/^[A-Z0-9._%-+]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z]{2,6}$/i';

    	if (!preg_match($pattern, $this->request->post['email'])) {
      		$this->error['email'] = $this->language->get('error_email');
    	}

    	if ((strlen(utf8_decode($this->request->post['telephone'])) < 1) || (strlen(utf8_decode($this->request->post['telephone'])) > 32)) {
      		$this->error['telephone'] = $this->language->get('error_telephone');
    	}
    	
    	/*
    	if ((strlen(utf8_decode($this->request->post['enquiry'])) < 10) || (strlen(utf8_decode($this->request->post['enquiry'])) > 3000)) {
      		$this->error['enquiry'] = $this->language->get('error_enquiry');
    	}

    	if (empty($this->request->post['subject'])) {
      		$this->error['subject'] = $this->language->get('error_subject');
    	}
		*/
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}  	  
  	} 
    
    private function getEmail(){
        if(stristr($this->config->get('config_email'), 'havhokeren.dk') !== false){
            $email = $this->config->get('config_email');                
        }
        else{
            $email = 'webshop@havhokeren.dk';
        }
        
        return $email;
    } 
}
?>
